package com.wanmait.recycle.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.pojo.Article;
import com.wanmait.recycle.pojo.Data;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.History;
import com.wanmait.recycle.pojo.Province;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.pojo.Unit;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.ArticleService;
import com.wanmait.recycle.service.DataService;
import com.wanmait.recycle.service.FirsttypeService;
import com.wanmait.recycle.service.HistoryService;
import com.wanmait.recycle.service.ProvinceService;
import com.wanmait.recycle.service.RubbishService;
import com.wanmait.recycle.service.RubbishUnitService;
import com.wanmait.recycle.service.SecondtypeService;
import com.wanmait.recycle.service.UnitService;
import com.wanmait.recycle.util.CalculateDataUtil;
import com.wanmait.recycle.util.UploadPhotosUtil;

@Controller
@RequestMapping("/web")
public class UserRubbishController {
	@Resource
	private RubbishService rubbishService;
	@Resource
	private UnitService unitService;
	@Resource
	private DataService dataService;
	@Resource
	private ArticleService articleService;
	@Resource
	private RubbishUnitService rubbishUnitService;
	@Resource
	private SecondtypeService secondtypeService;
	@Resource
	private FirsttypeService firsttypeService;
	@Resource
	private HistoryService historyService;
	@Resource
	private ProvinceService provinceService;
	@RequestMapping("rubbish/addRubbish")
	public ModelAndView UserRubbishAddPage(HttpServletRequest request){
		HttpSession session=request.getSession();
		Userinfo user=(Userinfo)session.getAttribute("userinfo");
		ModelAndView mv=new ModelAndView();
		List<Secondtype> secondtypes=secondtypeService.findAll();
		List<Firsttype> firsttypes=firsttypeService.findAll();
		List<History> userHistory=historyService.findByUserinfo(user);
		List<Unit> units=unitService.findAll();
		mv.addObject("secondtypes",secondtypes);
		mv.addObject("firsttypes",firsttypes);
		mv.addObject("units",units);
		mv.addObject("userHistory",userHistory);
		return mv;

	}
	@RequestMapping("rubbish/showHistory")
	public ModelAndView UsershowHistory(HttpServletRequest request){
		HttpSession session=request.getSession();
		Userinfo user=(Userinfo)session.getAttribute("userinfo");
		ModelAndView mv=new ModelAndView();
		PageInfo<History> userHistory=historyService.findByUserinfoAndPageInfo(user);		
		mv.addObject("userHistory",userHistory);
		return mv;

	}
	@RequestMapping("rubbish/addObject")
	public String addObject(String type,String objectName,String firsttypeId,String secondtypeId,@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request,String content,String unitString) throws Exception{
		
		Integer ntype=Integer.parseInt(type);
		Integer nfirsttypeId=Integer.parseInt(firsttypeId);
		Integer nsecondtypeId=Integer.parseInt(secondtypeId);
		StringBuilder picsb=new StringBuilder();
		
		String photoName = UploadPhotosUtil.getPhotoNameAndSave("/static/rubbish_img", file, request);
		picsb.append(photoName);
	
		String pic=picsb.toString();
		HttpSession session=request.getSession();
		Userinfo userinfo=(Userinfo)session.getAttribute("userinfo");
		if (ntype==1) {
			rubbishService.userAddRubbish(objectName,nfirsttypeId,nsecondtypeId,pic,content,userinfo,unitString);
			Integer objectId=rubbishService.findMaxId();
			historyService.addHistory(ntype,userinfo,objectId,objectName,nfirsttypeId,nsecondtypeId);
		}
		else {
			secondtypeService.userAddSecondtype(objectName,nfirsttypeId,pic,content,userinfo);
			Integer objectId=secondtypeService.findMaxId();
			historyService.addHistory(ntype,userinfo,objectId,objectName,nfirsttypeId,null);
		}
		return "redirect:/web/rubbish/addRubbish";
		

	}

	@RequestMapping("rubbish/delObject")
	public String delObject(Integer type,Integer objectId){
		
	
		if (type==1) {
			rubbishService.realDelete(objectId);
			historyService.deleteByTypeAndObjectId(type,objectId);
		}
		else {
			secondtypeService.realDelete(objectId);
			historyService.deleteByTypeAndObjectId(type,objectId);
		}
		return "redirect:/web/rubbish/addRubbish";
		

	}
	@RequestMapping("rubbish/delObject2")
	public String delObject2(Integer type,Integer objectId){
		
	
		if (type==1) {
			rubbishService.realDelete(objectId);
			historyService.deleteByTypeAndObjectId(type,objectId);
		}
		else {
			secondtypeService.realDelete(objectId);
			historyService.deleteByTypeAndObjectId(type,objectId);
		}
		return "redirect:/web/rubbish/showHistory";
		

	}
	@RequestMapping("rubbish/rubbishDetail")
	public ModelAndView userRubbishDetail(String rubbishId,String pageNum){
		if(pageNum==null) {
			pageNum="1";
		}
		Integer nPageNum=Integer.parseInt(pageNum);
		Integer nRubbishId=Integer.parseInt(rubbishId);
		ModelAndView mv=new ModelAndView();
		Rubbish rubbish=rubbishService.selectById(nRubbishId);
		PageInfo<Data> datas=dataService.findAllByRubbishId(nRubbishId);
		List<Province> provinces=provinceService.findAll();
		List<Article> articles=articleService.findArticleByRubbishIdpdz(nRubbishId);
		mv.addObject("datas",datas);
		mv.addObject("rubbish",rubbish);
		mv.addObject("articles",articles);
		mv.addObject("provinces",provinces);
		mv.addObject("pageNum",nPageNum);
		return mv;

	}
	@RequestMapping("rubbish/test")
	public ModelAndView userTest(){
		
		ModelAndView mv=new ModelAndView();
	
		
		return mv;

	}
	@ResponseBody
	@RequestMapping("data/dataStatistics")
	public List<Double> dataStatistics(String rubbishId,String cityId,String beginTime,String endTime){
		System.out.println("beginTime"+beginTime+",endTime"+endTime);
		if("".equals(beginTime)||beginTime==null||"undefined".equals(beginTime)) {
			beginTime="2000-01-01";
		}
		if("".equals(endTime)||endTime==null||"undefined".equals(endTime)) {
			endTime="2200-01-01";//那会儿我们肯定不在了
		}
		Integer nRubbishId=Integer.parseInt(rubbishId);
		Integer nCityId=Integer.parseInt(cityId);
		List<Double> results=new ArrayList<>();	
		List<Data> allDatas=dataService.findAll();
		Map<String,Double> map=CalculateDataUtil.getInstance().dataStatistics(nRubbishId,nCityId,beginTime,endTime,allDatas);
		results.add(map.get("disposeCount"));
		results.add(map.get("dischargeCount"));
		return results;

	}
	@ResponseBody
	@RequestMapping("data/dataStatisticsByYear")
	public List<Double> dataStatisticsByYear(String rubbishId,String cityId,String year){
		
		Integer nRubbishId=Integer.parseInt(rubbishId);
		Integer nCityId=Integer.parseInt(cityId);
		Integer nYear=Integer.parseInt(year);		
		List<Double> results=new ArrayList<>();
		List<Data> allDatas=dataService.findAll();
		Map<String,Double> map=null;
		for(int i=1;i<=11;i++) {
			String beginTime=nYear+"-"+i+"-01";
			String endTime=nYear+"-"+(i+1)+"-01";
			 map=CalculateDataUtil.getInstance().dataStatistics(nRubbishId,nCityId,beginTime,endTime,allDatas);
			results.add(map.get("disposeCount"));
			results.add(map.get("dischargeCount"));
		}
		String beginTime=nYear+"-"+"12"+"-01";
		String endTime=(nYear+1)+"-"+"01"+"-01";
		map=CalculateDataUtil.getInstance().dataStatistics(nRubbishId,nCityId,beginTime,endTime,allDatas);
		results.add(map.get("disposeCount"));
		results.add(map.get("dischargeCount"));
		
		return results;
	}
	@ResponseBody
	@RequestMapping("rubbish/judge")
	public Boolean judgement(String objectName,String secondTypeId) {
		if("undefined".equals(secondTypeId)) {
			
			return false;
		}
		Integer nSecondTypeId=Integer.parseInt(secondTypeId);
		if(nSecondTypeId==0) {
			
			return false;
		}
		if(nSecondTypeId==-1) {
			
			return secondtypeService.judgeIfRepetition(objectName);
		}
		
		return rubbishService.judgeIfRepetition(objectName,nSecondTypeId);
		
		
	}
}